# import pygame, random and math
import pygame as pg
import random as rd
import math

# init program
pg.init()
# set screen
screen = pg.display.set_mode((1000, 500))
screen.fill((255, 255, 255))
# set title
pg.display.set_caption("BallFight_Avaritia", "4.0")


# Chinese:pg.display.set_caption("球球大作战_无尽贪婪", "4.0")

# def circle
def circle(color, point, r, size):
    pg.draw.circle(screen, color, point, r, size)


# class ball
class Ball():
    def __init__(self):
        self.color = (rd.randint(0, 255), rd.randint(0, 255), rd.randint(0, 255))
        self.x = rd.randint(0, 1000)
        self.y = rd.randint(0, 500)
        self.r = rd.randint(5, 15)
        self.size = rd.randint(5, 15)


# make a balllist
balllist = []
for i in range(600):
    balllist.append(Ball())

# creat myball
myball = Ball()
myball.color = (0, 0, 0)
myball.x = 500
myball.y = 250
myball.size = 5
myball.speed = 10


# def check touch
# use the pythagorean theorem
def touch(myX, myY, fX, fY, myR, fR):
    distance = math.sqrt((myX - fX) ** 2 + (myY - fY) ** 2)
    if distance <= myR + fR:
        # just return True
        return True
    else:
        # return False
        return False


# def foodDelivery
def foodDelivery():
    time = pg.time.get_ticks()
    # every 10 seconds put 30 foods
    if time % 10000 >= 9000 and time % 10000 <= 9020:
        for i in range(30):
            balllist.append(Ball())


# def draw
# use "Ball" and for range to append in the balllist
def draw():
    for ball in balllist:
        if touch(myball.x, myball.y, ball.x, ball.y, myball.size, ball.size):
            balllist.remove(ball)
            myball.size += 0.1
            # make the speed to be smaller than the last one
            # use the multiplier scale decreases and inverse proportional function
            myball.speed *= 0.992
        else:
            circle(ball.color, (ball.x, ball.y), ball.size, 0)
    circle(myball.color, (myball.x, myball.y), myball.size, 0)


# check fps, do not quit program
fps = pg.time.Clock()
# check quit and play program
while True:
    # do not make the fps so high
    # if the fps is high, the computer will ~"bomb!"
    fps.tick(60)
    event = pg.event.poll()
    if event.type == pg.QUIT:
        pg.quit()
        exit()
    keys = pg.key.get_pressed()
    # make the ball to move
    # I use the "wasd"
    # also can use up down right left
    if keys[pg.K_w]:
        myball.y -= myball.speed
    if keys[pg.K_a]:
        myball.x -= myball.speed
    if keys[pg.K_s]:
        myball.y += myball.speed
    if keys[pg.K_d]:
        myball.x += myball.speed
    if keys[pg.K_UP]:
        myball.y -= myball.speed
    if keys[pg.K_DOWN]:
        myball.y += myball.speed
    if keys[pg.K_LEFT]:
        myball.x -= myball.speed
    if keys[pg.K_RIGHT]:
        myball.x += myball.speed
    # the e is to update ball's xy
    elif keys[pg.K_e]:
        myball.x, myball.y = 500, 250
    # draw and check
    draw()
    foodDelivery()
    # display program
    pg.display.update()
    screen.fill((255, 255, 255))